package com.cdshangpai.photoapp.ui.caseui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.cdshangpai.photoapp.PhotoAppBaseActivity;
import com.cdshangpai.photoapp.R;
import com.cdshangpai.photoapp.databinding.ActivityCaseMainBinding;
import com.cdshangpai.photoapp.ui.FilterAdapter;
import com.cdshangpai.photoapp.ui.FilterBean;
import com.cdshangpai.photoapp.ui.FilterPriceAdapter;
import com.cdshangpai.photoapp.ui.OnFilterItemClickListener;
import com.cdshangpai.photoapp.ui.PriceFilter;
import com.cdshangpai.utils.system.Keywboard;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

public class CaseMainActivity extends PhotoAppBaseActivity implements OnFilterItemClickListener {
    Fragment caseFragment = new Fragment();
    Fragment videoFragment;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    ArrayList<PriceFilter> priceFilters = initPrice();

    ActivityCaseMainBinding binding;

    public void gotoHome(View v) {
        this.finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_case_main);

        binding.caseListAreafilterView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        binding.caseListCityfilterView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        binding.caseListColorfilterView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        binding.caseListPricefilterView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        binding.caseListStylefilterView.setOverScrollMode(View.OVER_SCROLL_NEVER);



        binding.setFilteropen(false);
        binding.setCaseFilterMenuShow(false);
        initData();
        initView();
    }

    //查询案例
    public void onSearch() {
        String keyword = binding.mainCaseKeyword.getText().toString();
        ((CaseFragment) caseFragment).keywordSearch(keyword);
    }

    private void initData() {
        CaseListUiData caseListUiData = new CaseListUiData();
        binding.setData(caseListUiData);
    }

    private void initView() {
        binding.setView(this);

        caseFragment = new CaseFragment();
        videoFragment = new VideoFragment();

        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.case_main_contaner, caseFragment).add(R.id.case_main_contaner, videoFragment).commit();

        switchToCase();

        initColorFilterData();
        initAreaFilterData();
        initStyleFilterData();
        initPriceFilterData();
        initCityFilterData();

        binding.mainCaseKeyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    List<View> views = new ArrayList<>();
                    views.add(binding.mainCaseKeyword);
                    Keywboard.hideSoftKeyboard(getApplicationContext(), views);

                    onSearch();
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    public void selectMenu(View view) {
        switch (view.getId()) {
            case R.id.case_main_menu_case:
                switchToCase();
                break;
            case R.id.case_main_menu_video:
                switchToVidwo();
                break;
        }
    }

    private void switchToCase() {
        binding.setSelectMenuPos(0);
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(videoFragment).show(caseFragment).commit();
//        ((CaseFragment)caseFragment).search();
        binding.setSearchShow(true);
        binding.setCaseFilterMenuShow(true);
    }

    private void switchToVidwo() {
        binding.setSelectMenuPos(1);
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(caseFragment).show(videoFragment).commit();
//        ((VideoFragment)videoFragment).search();
        binding.setSearchShow(false);
        binding.setCaseFilterMenuShow(false);
    }

    //案例查询相关代码
    @Override
    public void onItemClick(View view, String name, Integer position) {
        ArrayList<FilterBean> list;
        CaseFragment frag = (CaseFragment) caseFragment;
        switch (name) {
            case "area":
                /*list = binding.getData().areaAdapter.datas;
                processFilterSelect(binding.caseListAreafilterView.getAdapter(), list, position);
                if (position == list.size() - 1) {
                    frag.caseFilter.area = "";
                } else {
                    frag.caseFilter.area = list.get(position).text;
                }*/
                list = binding.getData().areaAdapter.datas;
                processCityFilterSelect(binding.getData().areaAdapter, list, position);

                ArrayList<String> areas = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    FilterBean filterBean = list.get(i);
                    if (filterBean.checked) {
                        areas.add(filterBean.text);
                    }
                }
                frag.caseFilter.area = new String[areas.size()];
                areas.toArray(frag.caseFilter.area);
                break;
            case "color":
                list = binding.getData().colorAdapter.datas;
                processCityFilterSelect(binding.getData().colorAdapter, list, position);

                ArrayList<String> colors = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    FilterBean filterBean = list.get(i);
                    if (filterBean.checked) {
                        colors.add(filterBean.text);
                    }
                }
                frag.caseFilter.color = new String[colors.size()];
                colors.toArray(frag.caseFilter.color);
                break;
            case "style":
                list = binding.getData().styleAdapter.datas;
                processCityFilterSelect(binding.getData().styleAdapter, list, position);

                ArrayList<String> styles = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    FilterBean filterBean = list.get(i);
                    if (filterBean.checked) {
                        styles.add(filterBean.text);
                    }
                }
                frag.caseFilter.style = new String[styles.size()];
                styles.toArray(frag.caseFilter.style);
                break;
            case "price":
//                list = binding.getData().priceAdapter.datas;
//                processFilterSelect(binding.caseListPricefilterView.getAdapter(), list, position);
//
//                if (position == list.size() - 1) {
//                    frag.caseFilter.fromPrice = null;
//                    frag.caseFilter.toPrice = null;
//                } else {
//                    PriceFilter priceFilter = priceFilters.get(position);
//                    frag.caseFilter.fromPrice = priceFilter.fromPrice;
//                    frag.caseFilter.toPrice = priceFilter.toPrice;
//                }
                list = binding.getData().priceAdapter.datas;
                processCityFilterSelect(binding.getData().priceAdapter, list, position);

                ArrayList<String> prices = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    FilterBean filterBean = list.get(i);
                    if (filterBean.checked) {
                        prices.add(filterBean.text);
                    }
                }
                frag.caseFilter.price = new String[prices.size()];
                prices.toArray(frag.caseFilter.price);
                break;
            case "city":
                list = binding.getData().cityAdapter.datas;
                processCityFilterSelect(binding.getData().cityAdapter, list, position);

                ArrayList<String> citys = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    FilterBean filterBean = list.get(i);
                    if (filterBean.checked) {
                        citys.add(filterBean.text);
                    }
                }
                frag.caseFilter.city = new String[citys.size()];
                citys.toArray(frag.caseFilter.city);
                break;
        }
        frag.search();
    }

    private ArrayList<PriceFilter> initPrice() {
        ArrayList<PriceFilter> priceFilters = new ArrayList<>();
        priceFilters.add(new PriceFilter(null, 5));
        priceFilters.add(new PriceFilter(5, 8));
        priceFilters.add(new PriceFilter(8, 12));
        priceFilters.add(new PriceFilter(12, 15));
        priceFilters.add(new PriceFilter(15, 20));
        priceFilters.add(new PriceFilter(20, 25));
        priceFilters.add(new PriceFilter(25, 30));
        priceFilters.add(new PriceFilter(30, null));
        return priceFilters;
    }

    private FlexboxLayoutManager getFlexboxLayoutManager(Boolean isCenter) {
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this.getApplicationContext());
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        if (isCenter) {
            layoutManager.setJustifyContent(JustifyContent.CENTER);
            layoutManager.setAlignItems(AlignItems.CENTER);
        } else {
            layoutManager.setAlignItems(AlignItems.STRETCH);
            layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        }
        return layoutManager;
    }

    private void initColorFilterData() {
        FilterAdapter filterAdapter = new FilterAdapter(this.getApplicationContext());
        filterAdapter.name = "color";
        filterAdapter.onFilterItemClickListener = this;

        filterAdapter.datas = new ArrayList<>();
        filterAdapter.datas.add(new FilterBean("白绿", false));
        filterAdapter.datas.add(new FilterBean("红色", false));
        filterAdapter.datas.add(new FilterBean("粉色", false));
        filterAdapter.datas.add(new FilterBean("蓝色", false));
        filterAdapter.datas.add(new FilterBean("撞色", false));
        filterAdapter.datas.add(new FilterBean("橙黄", false));
        filterAdapter.datas.add(new FilterBean("紫色", false));
        filterAdapter.datas.add(new FilterBean("咖色", false));
//        filterAdapter.datas.add(new FilterBean("不限", true));

        FlexboxLayoutManager layoutManager = getFlexboxLayoutManager(true);
        binding.caseListColorfilterView.setLayoutManager(layoutManager);

        CaseListUiData caseListUiData = binding.getData();
        caseListUiData.colorAdapter = filterAdapter;
        binding.setData(caseListUiData);

        binding.caseListColorfilterView.setAdapter(filterAdapter);

        ((SimpleItemAnimator) binding.caseListColorfilterView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    private void initAreaFilterData() {
        FilterAdapter filterAdapter = new FilterAdapter(this.getApplicationContext());
        filterAdapter.name = "area";
        filterAdapter.onFilterItemClickListener = this;

        filterAdapter.datas = new ArrayList<>();
        filterAdapter.datas.add(new FilterBean("户外", false));
        filterAdapter.datas.add(new FilterBean("室内", false));
//        filterAdapter.datas.add(new FilterBean("不限", true));

        FlexboxLayoutManager layoutManager = getFlexboxLayoutManager(true);
        binding.caseListAreafilterView.setLayoutManager(layoutManager);

        CaseListUiData caseListUiData = binding.getData();
        caseListUiData.areaAdapter = filterAdapter;
        binding.setData(caseListUiData);

        binding.caseListAreafilterView.setAdapter(filterAdapter);
        ((SimpleItemAnimator) binding.caseListAreafilterView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    private void initStyleFilterData() {
        FilterAdapter filterAdapter = new FilterAdapter(this.getApplicationContext());
        filterAdapter.name = "style";
        filterAdapter.onFilterItemClickListener = this;

        filterAdapter.datas = new ArrayList<>();
        filterAdapter.datas.add(new FilterBean("森系", false));
        filterAdapter.datas.add(new FilterBean("现代", false));
        filterAdapter.datas.add(new FilterBean("韩式", false));
        filterAdapter.datas.add(new FilterBean("花园", false));
        filterAdapter.datas.add(new FilterBean("求婚", false));
        filterAdapter.datas.add(new FilterBean("中式", false));
        filterAdapter.datas.add(new FilterBean("欧式", false));
        filterAdapter.datas.add(new FilterBean("宝宝宴", false));
//        filterAdapter.datas.add(new FilterBean("不限", true));

        FlexboxLayoutManager layoutManager = getFlexboxLayoutManager(true);
        binding.caseListStylefilterView.setLayoutManager(layoutManager);

        CaseListUiData caseListUiData = binding.getData();
        caseListUiData.styleAdapter = filterAdapter;
        binding.setData(caseListUiData);

        binding.caseListStylefilterView.setAdapter(filterAdapter);

        ((SimpleItemAnimator) binding.caseListStylefilterView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    private void initCityFilterData() {
        FilterAdapter filterAdapter = new FilterAdapter(this.getApplicationContext());
        filterAdapter.name = "city";
        filterAdapter.onFilterItemClickListener = this;

        filterAdapter.datas = new ArrayList<>();
        filterAdapter.datas.add(new FilterBean("成都", false));
        filterAdapter.datas.add(new FilterBean("北京", false));
        filterAdapter.datas.add(new FilterBean("三亚", false));

        FlexboxLayoutManager layoutManager = getFlexboxLayoutManager(true);
        binding.caseListCityfilterView.setLayoutManager(layoutManager);

        CaseListUiData caseListUiData = binding.getData();
        caseListUiData.cityAdapter = filterAdapter;
        binding.setData(caseListUiData);

        binding.caseListCityfilterView.setAdapter(filterAdapter);

        ((SimpleItemAnimator) binding.caseListCityfilterView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    private void initPriceFilterData() {
        FilterPriceAdapter filterAdapter = new FilterPriceAdapter(this.getApplicationContext());
        filterAdapter.name = "price";
        filterAdapter.onFilterItemClickListener = this;

        filterAdapter.datas = new ArrayList<>();
        filterAdapter.datas.add(new FilterBean("5w以内", false));
        filterAdapter.datas.add(new FilterBean("5w~8w", false));
        filterAdapter.datas.add(new FilterBean("8w~12w", false));
        filterAdapter.datas.add(new FilterBean("12w~15w", false));
        filterAdapter.datas.add(new FilterBean("15w~20w", false));
        filterAdapter.datas.add(new FilterBean("20w~25w", false));
        filterAdapter.datas.add(new FilterBean("25w~30w", false));
        filterAdapter.datas.add(new FilterBean("30w以上", false));
//        filterAdapter.datas.add(new FilterBean("不限", true));

        FlexboxLayoutManager layoutManager = getFlexboxLayoutManager(true);
        binding.caseListPricefilterView.setLayoutManager(layoutManager);

        CaseListUiData caseListUiData = binding.getData();
        caseListUiData.priceAdapter = filterAdapter;
        binding.setData(caseListUiData);

        binding.caseListPricefilterView.setAdapter(filterAdapter);

        ((SimpleItemAnimator) binding.caseListPricefilterView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    private void processCityFilterSelect(RecyclerView.Adapter adapter, ArrayList<FilterBean> list, Integer position) {
        FilterBean clickBean = list.get(position);
        clickBean.checked = !clickBean.checked;
        adapter.notifyItemChanged(position);
    }

    private FilterBean processFilterSelect(RecyclerView.Adapter adapter, ArrayList<FilterBean> list, Integer position) {
        FilterBean clickBean = list.get(position);
        if (!clickBean.checked) {
            for (int i = 0; i < list.size(); i++) {
                if (i != position && list.get(i).checked) {
                    list.get(i).checked = false;
                    adapter.notifyItemChanged(i);
                    break;
                }
            }
        }
        clickBean.checked = true;
        adapter.notifyItemChanged(position);
        return clickBean;
    }

    public void switchFilter(View v) {
        Boolean open = !(binding.getFilteropen());
        binding.setFilteropen(open);

        binding.setView(this);
    }

    public Boolean getFilterOpenState() {
        return binding.getFilteropen();
    }
}